package com.xiaoyang.forum.common.code;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

/**
 * @Author: XiaoYang
 * @CreateTime: 2025-08-10
 * @Description: 统一返回结果类
 */
@Getter
@Setter
public class Result<T> {
    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private int code; // 状态码

    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private String message; // 状态描述信息

    @JsonInclude(JsonInclude.Include.ALWAYS) // 不论任何情况都参与JSON序列化
    private T data; // 数据

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result(int code, String message) {
        this(code, message, null);
    }

    // ==== 请求成功 ====
    public static Result success() {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static Result success(String message) {
        return new Result(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T> Result<T> success(T data) {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> success(String message, T data) {
        return new Result(ResultCode.SUCCESS.getCode(), message, data);
    }

    public static Result failed() {
        return new Result(ResultCode.FAILED.getCode(),
                ResultCode.FAILED.getMessage());
    }

    public static <T> Result<T> failed(String message) {
        return new Result(ResultCode.FAILED.getCode(), message);
    }

    public static <T> Result<T> failed(T data) {
        return new Result(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), data);
    }

    public static Result failed(ResultCode resultCode) {
        return new Result(resultCode.getCode(), resultCode.getMessage());
    }
}
